﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace ChessOnline
{
    public partial class HomePage : Page
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void HomePage_Loaded(object sender, RoutedEventArgs e)
        {
            Authorization.Instance.ThrowLoginEnded += new Authorization.EventHandler(LoginEnded);
            Authorization.Instance.ThrowAuthorizationCompleted += new Authorization.EventHandler(AuthorizationCheckEnded);
            Authorization.Instance.CheckAuthorization();
        }

        private void AuthorizationCheckEnded(object sender, object args)
        {
            if (Authorization.Instance.IsAuthorized())
            {
                Navigator.Instance.Go(this, "GamesPage.xaml");
            }
        }

        private void LoginEnded(object sender, object args)
        {
            if (Authorization.Instance.GetUserHash() != "UserNotFound")
            {
                Authorization.Instance.CheckAuthorization();
            }
            else
            {
                errorLoginLabel.Opacity = 1.0f;
            }
        }

        private void RegistrationButton_Clicked(object sender, RoutedEventArgs e)
        {
            Navigator.Instance.Go(this, "RegistrationPage.xaml");
        }

        private void AuthorizationButton_Clicked(object sender, RoutedEventArgs e)
        {
            Authorization.Instance.LogIn(loginBox.Text, passwordBox.Password);
        }

        private void HomePage_Unloaded(object sender, RoutedEventArgs e)
        {
            // Отписаться от событий авторизации
            Authorization.Instance.ThrowLoginEnded -= new Authorization.EventHandler(LoginEnded);
            Authorization.Instance.ThrowAuthorizationCompleted -= new Authorization.EventHandler(AuthorizationCheckEnded);
        }
    }
}
